package com.ruoyi.web.controller.app.user;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.app.domain.bo.AppUserBo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 实名认证审核
 *
 * @author heise
 * @date 2025-06-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/userReal")
public class AppUserRealController extends BaseController {

    private final IAppUserService appUserService;
    private final ISysConfigService sysConfigService;


    /**
     * 实名认证审核
     */
    @PostMapping("/add")
    public R<Void> add(AppUserBo bo) {
        Long id = getUserId();
        AppUserVo appUserVo = appUserService.queryById(id);
        if (StringUtils.isBlank(bo.getCode()) || StringUtils.isBlank(bo.getCodeName())) {
            return R.fail("请填写姓名和身份证号！");
        }
        if (StringUtils.isNotBlank(appUserVo.getCode())) {
            return R.fail("已实名认证，请勿重复操作！");
        }
        List<AppUserVo> codeUser = appUserService.queryByCode(bo.getCode());
        if (!codeUser.isEmpty()) {
            return R.fail("身份证信息已经被认证！");
        }
        bo.setId(id);
        if (appUserService.updateByBo(bo)) {
            String ztRewards = sysConfigService.selectConfigByKey("app.zhitui.integral");
            String jtRewards = sysConfigService.selectConfigByKey("app.jiantui.integral");
            appUserService.recommendedRewards(id, ztRewards, jtRewards);
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 获取实名认证详细信息
     */
    @GetMapping("/details")
    @SaIgnore
    public R<?> details() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", 3);
        AppUserVo appUserVo = appUserService.queryById(getUserId());
        if (StringUtils.isNotBlank(appUserVo.getCode())) {
            map.put("status", 1);
            map.put("code", appUserVo.getCode());
            map.put("codeName", appUserVo.getCodeName());
        }
        return R.ok(map);
    }
}
